package com.dinggao.crossgo.wxapi;

import com.dinggao.crossgo.core.constant.CommonConstant;
import com.dinggao.crossgo.core.manage.ApiManager;
import com.dinggao.crossgo.core.observer.EventIntet;
import com.dinggao.crossgo.core.observer.EventMsg;
import com.dinggao.crossgo.core.paser.FastJsonUtils;
import com.dinggao.crossgo.core.util.LogUtils;
import com.dinggao.crossgo.core.util.SharedPreferencesUtil;
import com.dinggao.crossgo.module.roadbook.activity.RoadBookPublishMainActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.mm.opensdk.utils.Log;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI wxAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        wxAPI = WXAPIFactory.createWXAPI(this, CommonConstant.WECHAT_APPID, true);
        wxAPI.registerApp(CommonConstant.WECHAT_APPID);
        wxAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        wxAPI.handleIntent(getIntent(), this);
        Log.e("ansen", "WXEntryActivity onNewIntent");
    }

    @Override
    public void onReq(BaseReq arg0) {
        Log.e("ansen", "WXEntryActivity onReq:" + arg0);
    }

    @Override
    public void onResp(BaseResp resp) {

        int type = resp.getType();//类型，分享登录

        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                //用户拒绝授权
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                finish();
                //取消授权
                break;
            case BaseResp.ErrCode.ERR_OK:
                //授权
                SendAuth.Resp authResp = (SendAuth.Resp) resp;

                String code=authResp.code;
                Log.e("ansen", "微信登录操作....." +code);

                //网络请求获取access_token和用户个人信息

                getToken(code);

                break;
        }



    }

    private void getToken(String code) {


            OkGo.<String>get(ApiManager.WXTOKEAN)
                .tag(this)
                .params("appid",CommonConstant.WECHAT_APPID)
                .params("secret",CommonConstant.WECHAT_SECRET)
                .params("code",code)
                .params("grant_type","authorization_code")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.e("response",response.body());

                        WxToken wxToken = FastJsonUtils.getObject(response.body(), WxToken.class);
                        String access_token = wxToken.getAccess_token();
                        String openid = wxToken.getOpenid();
                        getUserInfo(access_token,openid);
                    }
                });


    }

    private void getUserInfo(String access_token, String openid){
        OkGo.<String>get(ApiManager.WXUserInfo)
                .tag(this)
                .params("access_token", access_token)
                .params("openid", openid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        LogUtils.e("response", response.body());
                        WxUserInfo wxUserInfo=FastJsonUtils.getObject(response.body(),WxUserInfo.class);


                        SharedPreferencesUtil.setSharedPreferencesString(
                                WXEntryActivity.this,
                                CommonConstant.USERINFO_SHREDPREFERENCE,
                                CommonConstant.USERINFO_HEANDIMAGEURI,
                                wxUserInfo.getHeadimgurl());

                        SharedPreferencesUtil.setSharedPreferencesString(
                                WXEntryActivity.this,
                                CommonConstant.USERINFO_SHREDPREFERENCE,
                                CommonConstant.USERINFO_NICKNAME,
                                wxUserInfo.getNickname());

                        SharedPreferencesUtil.setSharedPreferencesBoolean(
                                WXEntryActivity.this,
                                CommonConstant.USERINFO_SHREDPREFERENCE,
                                CommonConstant.USERINFO_USEWEIXIN,
                                true);

                        EventIntet.getDefault().post(new EventMsg("WXEntryActivity", 9999, String.valueOf(getClass().getName())));
                        Intent intent = new Intent();
                        intent.setClass(WXEntryActivity.this, RoadBookPublishMainActivity.class);
                        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);

                    }
                });
    }

}